﻿using Loong.Admin.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Volo.Abp.AspNetCore.Mvc.ExceptionHandling;
using Volo.Abp.Http;

namespace Loong.Admin.ExceptionHandling
{
    public class AdminExceptionFilter : AbpExceptionFilter
    {
        protected override async Task HandleAndWrapException(ExceptionContext context)
        {
            await base.HandleAndWrapException(context);

            var result = context.Result as ObjectResult;
            if (result != null)
            {
                var error = result.Value as RemoteServiceErrorResponse;
                context.Result = new ObjectResult(new AjaxResponse
                {
                    Error = error?.Error,
                    Success = false,
                    Message = error?.Error?.Message
                });
            }
        }
    }
}